package stu.sup.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.dto.BuycarDto;
import stu.sup.entity.Buycart;
import stu.sup.entity.Product;
import stu.sup.entity.UserLogin;
import stu.sup.mapper.BuycartMapper;
import stu.sup.mapper.ProductMapper;
import stu.sup.mapper.UserLoginMapper;
import stu.sup.service.IBuycarService;

import java.util.Date;
import java.util.List;


/**
 * Created by HUAWEI on 2020/6/22.
 */
@Service
@Transactional
public class BuyCarServiceImpl implements IBuycarService{
    @Autowired
    private BuycartMapper buycartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Override
    public Buycart addProduct(Buycart buycart){
        buycartMapper.insert(buycart);
        return buycart;
    }

    @Override
    public BuycarDto showproduct(Integer ProductId, Integer UserId, Integer num, Double price, Date date) {
        Product product=productMapper.selectByPrimaryKey(ProductId);
        UserLogin userLogin=userLoginMapper.selectByPrimaryKey(UserId);
        BuycarDto buycarDto=new BuycarDto();
        buycarDto.setProduct(product);
        buycarDto.setUserLogin(userLogin);
        buycarDto.setAddTime(date);
        buycarDto.setProductPrice(price);
        buycarDto.setProductNum(num);
        return buycarDto;
    }


    @Override
    public boolean flagbuycar(Integer userId, Integer productId) {
        List<Buycart> list=buycartMapper.selectByUserId(userId);

        boolean flag=false;
        if(null==list || list.size()==0)
            flag = false;
        else{
            for (Buycart buycart : list) {
                if(buycart.getProductId().equals(productId)){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    @Override
    public double Allprice(Integer UserId) {
        List<Buycart> buycarts=buycartMapper.selectByUserId(UserId);
        double price=0;
        for (Buycart b:buycarts){
            price=price+b.getProductPrice()*b.getProductNum();
        }
        return price;
    }

    @Override
    public void delatebuycar(Buycart buycart) {
        buycartMapper.deleteByPrimaryKey(buycart.getBuycartId());
    }

    @Override
    public List<Buycart> findByuser(int pageNum, int pageSize, int userloginId) {
        PageHelper.startPage(pageNum, pageSize);
        return buycartMapper.selectByUserId(userloginId);
    }


}
